<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/database.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/profile.php';

	//If user exist
	if (isset($user)) {
		//Get gender
		$gender = "Male";
		$result = mysql_query("SELECT * FROM genders WHERE id=". $user['gender']) or die(mysql_error());
		if (mysql_num_rows($result) >= 1) {
			$row	= mysql_fetch_array($result);
			$gender	= $row['name'];
		}

		//Get avatar
		$avatar = "/deviantpic/picture/files/unknown.jpg";
		if ($user['avatar_id'] > 0) {
			$result = mysql_query("SELECT photo FROM photos WHERE id='" . $user['avatar_id'] . "'");
			$row	= mysql_fetch_array($result);
			$avatar	= $row['photo'];
		}

		//Get wall
		$wall = "Everybody";
		$result = mysql_query("SELECT * FROM wall_privileges WHERE id=". $user['wall_privilege']) or die(mysql_error());
		if (mysql_num_rows($result) >= 1) {
			$row	= mysql_fetch_array($result);
			$wall	= $row['name'];
		}

		//Get followers
		$followers = array();
		$result = mysql_query("SELECT follower_id FROM followers WHERE user_id='" . $user['id'] . "'");
		if (mysql_num_rows($result) >= 1) {
			//While there's still data
			$row = mysql_fetch_array($result);
			while ($row) {
				//Get user
				$user_result	= mysql_query("SELECT * FROM users WHERE id='" . $row['follower_id'] . "'");
				$followers[]	= mysql_fetch_array($user_result);

				//next
				$row = mysql_fetch_array($result);
			}
		}

		//Get followings
		$followings = array();
		$result = mysql_query("SELECT user_id FROM followers WHERE follower_id='" . $user['id'] . "'");
		if (mysql_num_rows($result) >= 1) {
			//While there's still data
			$row = mysql_fetch_array($result);
			while ($row) {
				//Get user
				$user_result	= mysql_query("SELECT * FROM users WHERE id='" . $row['user_id'] . "'");
				$followings[]	= mysql_fetch_array($user_result);

				//next
				$row = mysql_fetch_array($result);
			}
		}
	}
?>

				<!-- Profile -->
				<div id="profile">
					<div class="tabs-title" id="profile-title">
						<span class="title">Profile</span>
						<span class="profile-edit"> 
							<?php
								//if user is the one logged in, edit
								if (isset($_SESSION['user']) && $user['id'] == $_SESSION['user'])
									echo "<a " . create_anchor ("edit", null) . ">edit</a>";
							?>
						</span>
					</div>
	
					<div class="avatar-picture">
						<img alt="avatar" width ="200" height="200" src=<?php echo '"' . $avatar . '"' ?> />
					</div>
	
					<!-- Real name -->
					<div class="profile-data">
						<span class="profile-parameter">Real name: </span>
						<span class="profile-value"><?php echo $user['realname']; ?></span>
					</div>
					
					<!-- Email -->
					<div class="profile-data">
						<span class="profile-parameter">E-mail address: </span>
						<span class="profile-value"><?php echo $user['email']; ?></span>
					</div>
	
					<!-- Birth date -->
					<div class="profile-data">
						<span class="profile-parameter">Birth date: </span>
						<span class="profile-value"><?php echo $user['birth']; ?></span>
					</div>
	
					<!-- Gender -->
					<div class="profile-data">
						<span class="profile-parameter">Gender: </span>
						<span class="profile-value"><?php echo $gender; ?></span>
					</div>
	
					<!-- Location -->
					<div class="profile-data">
						<span class="profile-parameter">Location: </span>
						<span class="profile-value"><?php echo $user['location']; ?></span>
					</div>
	
					<!-- Wall privilege -->
					<div class="profile-data">
						<span class="profile-parameter">Who can see write on your wall: </span>
						<span class="profile-value"><?php echo $wall; ?></span>
					</div>
	
					<!-- Followers -->
					<div class="profile-followers">
						<span class="profile-parameter">Followers: </span>
						<div id="profile-followers">
							<ul class="profile-followers">
								<?php
									foreach($followers as $follower) {
								?>
								<div class="profile-follower">
									<li class="profile-follower">
										<?php 
											echo '<a href="index.php?id=' . $follower['id'] . '">' . $follower['username'] . '</a>';
											echo ' <a href="chat.php?id=' . $follower['id'] . '">chat</a>';
										?>
									</li>
								</div>
								<?php
									}
								?>
							</ul>
						</div>
					</div>
					
				</div>

				<!-- Followers -->
				<div class="profile-followings">
					<span class="profile-parameter">Following: </span>
					<div id="profile-followings">
						<ul class="profile-followings">
							<?php
								foreach($followings as $following) {
							?>
							<div class="profile-following">
								<li class="profile-following">
									<?php
										echo '<a href="index.php?id=' . $following['id'] . '">' . $following['username'] . '</a>';
										echo ' <a href="chat.php?id=' . $following['id'] . '">chat</a>';
									?>
								</li>
							</div>
							<?php
								}
							?>
						</ul>
					</div>
				</div>